﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Web;

namespace Shuba.Web.master
{
    public partial class rkp_officer_unit : basePage
    {
        bool IsView = false;
        string msg = "";

        private void BindData()
        {
            DateTime StartDate = Convert.ToDateTime(txtStartDate.Text);
            DateTime EndDate = Convert.ToDateTime(txtToDate.Text);
            string UnitCode = ddlUnit.SelectedValue;

            Shuba.Business.Reports _Report = new Shuba.Business.Reports();
            DataSet ds = _Report.RPT_REKAP_PER_OFFICER_PER_UNIT(UnitCode, StartDate, EndDate);
            DataView source = ds.Tables[0].DefaultView;

            if (ViewState["SortEx"].ToString().Trim() != "")
            {
                string sort = sort = ViewState["SortEx"].ToString() + " " + ViewState["Sort"];
                source.Sort = sort;
            }

            gvList.DataSource = source;
            gvList.DataBind();

            if (gvList.Rows.Count > 0) btnExcel.Enabled = true; else btnExcel.Enabled = false;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string acl = GetUserAccess("RKP_OFFICER_UNIT");

            IsView = IsUserView(acl);

            if (!IsView)
            {
                msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
            }

            if (!IsPostBack)
            {
                txtStartDate.Text = ProcessingDate().ToString("dd MMMM yyyy");
                txtToDate.Text = ProcessingDate().ToString("dd MMMM yyyy");

                InitRegionList();
                InitClusterList(ddlRegion.SelectedValue);
                InitUnitList(ddlRegion.SelectedValue, ddlCluster.SelectedValue);

                InitSort();

                LabelTitle.Text = GetPageTitle("RKP_OFFICER_UNIT");                
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (Convert.ToDateTime(txtToDate.Text) >= Convert.ToDateTime(txtStartDate.Text))
            {
                BindData();
            }
            else
            {
                string msg = this.GetGlobalResourceObject("Messages", "InvalidDateRange").ToString();
                Alert(msg);
            }
        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvList.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection(e.SortExpression,e.SortDirection.ToString());
            ViewState["SortEx"] = e.SortExpression;
            gvList.PageIndex = 0;
            BindData();
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            if (Convert.ToDateTime(txtToDate.Text) >= Convert.ToDateTime(txtStartDate.Text))
            {
                System.Web.UI.ScriptManager.RegisterStartupScript(Page, this.GetType(), "Alert", "PrintThis();", true);
            }
            else
            {
                string msg = this.GetGlobalResourceObject("Messages", "InvalidDateRange").ToString();
                Alert(msg);
            }
        }

        private void InitUnitList(string RegionID, string ClusterID)
        {
            var bl = new Shuba.Business.Branch();

            if (RegionID == "ALL") RegionID = "%";
            if (ClusterID == "ALL") ClusterID = "%";

            ddlUnit.DataTextField = "BranchName";
            ddlUnit.DataValueField = "BranchID";
            ddlUnit.DataSource = bl.GetListByRegionAndCluster(RegionID, ClusterID, HttpContext.Current.User.Identity.Name, ConfigurationManager.AppSettings["app.code"]);
            ddlUnit.DataBind();

        }

        private void InitClusterList(string RegionalID)
        {
            var _Cluster = new Shuba.Business.Cluster();

            if (RegionalID == "ALL") RegionalID = "%";

            ddlCluster.DataTextField = "ClusterName";
            ddlCluster.DataValueField = "ClusterID";
            ddlCluster.DataSource = _Cluster.GetListByRegion(RegionalID, Session["UnitID"].ToString(), HttpContext.Current.User.Identity.Name, ConfigurationManager.AppSettings["app.code"]);
            ddlCluster.DataBind();

            if (ddlCluster.Items.Count > 1)
            {
                ListItem ListAll = new ListItem();
                ListAll.Value = "ALL";
                ListAll.Text = " = ALL CLUSTER = ";
                ddlCluster.Items.Insert(0, ListAll);
            }
        }

        private void InitRegionList()
        {
            var _Region = new Shuba.Business.Regional();

            ddlRegion.DataTextField = "RegionalName";
            ddlRegion.DataValueField = "RegionalID";
            ddlRegion.DataSource = _Region.GetList("%", Session["UnitID"].ToString(), HttpContext.Current.User.Identity.Name, ConfigurationManager.AppSettings["app.code"]);
            ddlRegion.DataBind();

            if (ddlRegion.Items.Count > 1)
            {
                ListItem ListAll = new ListItem();
                ListAll.Value = "ALL";
                ListAll.Text = " = ALL REGION = ";

                ddlRegion.Items.Insert(0, ListAll);
            }
        }

        protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitClusterList(ddlRegion.SelectedValue);
            InitUnitList(ddlRegion.SelectedValue, ddlCluster.SelectedValue);
        }

        protected void ddlCluster_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitUnitList(ddlRegion.SelectedValue, ddlCluster.SelectedValue);
        }

        protected void ddlUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlUnit.Items.Count == 0)
            {
                btnSearch.Enabled = false;
                btnExcel.Enabled = false;
            }
            else
            {
                btnSearch.Enabled = true;
                btnExcel.Enabled = true;
            }
        }
    }   
 }

